/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.iteminfo;

import com.aionengine.gameserver.model.gameobjects.Item;
import com.aionengine.gameserver.model.items.IdianStone;
import com.aionengine.gameserver.model.items.ItemStone;
import com.aionengine.gameserver.model.items.ManaStone;
import com.aionengine.gameserver.network.aion.iteminfo.ItemInfoBlob.ItemBlobType;

import java.nio.ByteBuffer;
import java.util.Set;

/**
 * This blob sends info about mana stones.
 *
 * @author -Nemesiss-
 * @modified Rolandas
 */
public class ManaStoneInfoBlobEntry extends ItemBlobEntry {

    ManaStoneInfoBlobEntry() {
        super(ItemBlobType.MANA_SOCKETS);
    }

    @Override
    public void writeThisBlob(ByteBuffer buf) {
        Item item = ownerItem;

        writeC(buf, item.isSoulBound() ? 1 : 0);
        writeC(buf, item.getEnchantLevel()); // enchant (1-15)
        writeD(buf, item.getItemSkinTemplate().getTemplateId());
        writeC(buf, item.getOptionalSocket());
        writeC(buf, 0);

        writeItemStones(buf);

        ItemStone god = item.getGodStone();
        writeD(buf, god == null ? 0 : god.getItemId());

        int itemColor = item.getItemColor();
        int dyeExpiration = item.getColorTimeLeft();
        // expired dyed items
        if ((dyeExpiration > 0 && item.getColorExpireTime() > 0 || dyeExpiration == 0 && item.getColorExpireTime() == 0)
                && item.getItemTemplate().isItemDyePermitted()) {
            writeC(buf, itemColor == 0 ? 0 : 1);
            writeD(buf, itemColor);
            writeD(buf, 0); // unk 1.5.1.9
            writeD(buf, dyeExpiration); // seconds until dye expires
        } else {
            writeC(buf, 0);
            writeD(buf, 0);
            writeD(buf, 0); // unk 1.5.1.9
            writeD(buf, 0);
        }
        IdianStone idianStone = item.getIdianStone();
        if (idianStone != null && idianStone.getPolishNumber() > 0) {
            writeD(buf, idianStone.getItemId()); // Idian Stone template ID
            writeC(buf, idianStone.getPolishNumber()); // polish statset ID
        } else {
            writeD(buf, 0); // Idian Stone template ID
            writeC(buf, 0); // polish statset ID
        }

    }

    /**
     * Writes manastones
     *
     * @param item
     */
    private void writeItemStones(ByteBuffer buf) {
        Item item = ownerItem;
        int count = 0;

        if (item.hasManaStones()) {
            Set<ManaStone> itemStones = item.getItemStones();

            for (ManaStone itemStone : itemStones) {
                if (count == 6)
                    break;
                writeD(buf, itemStone.getItemId());
                count++;
            }
            skip(buf, (6 - count) * 4);
        } else {
            skip(buf, 24);
        }
    }

    @Override
    public int getSize() {
    	return 12 * 2 + 24 + 1 + 5;
    }
}
